package com.cn.xiaonuo.sys.modular.video;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.xiaonuo.core.context.login.LoginContextHolder;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.login.SysLoginUser;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.sys.modular.video.mapper.VideoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/video/")
@Controller
public class videoController {


    @Autowired
    private VideoMapper videoMapper;

    @GetMapping("index")
    public String list() {
        return "business/video/index.html";
    }

    @GetMapping("form")
    public String form() {
        return "business/video/form.html";
    }
    /**
     * 查看详情
     * @return
     */
    @GetMapping("/detailPage")
    public String detailPage() {
        return "business/video/detail.html";
    }


    /**
     * 分页查询
     * @param videoVo
     * @return
     */
    @GetMapping("page")
    @ResponseBody
    public PageResult<Video> findVideoPage(VideoVo videoVo) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(videoVo)) {
            if (!StringUtils.isEmpty(videoVo.getTitle())) {
                queryWrapper.eq("title", videoVo.getTitle());
            }

            if (videoVo.getType() != null) {
                queryWrapper.eq("type", videoVo.getType());
            }
        }
        Page<Video> videoPage = videoMapper.selectPage(PageFactory.defaultPage(), queryWrapper);
        return new PageResult<>(videoPage);
    }

    /**
     *   新增
     * @param video
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public ResponseData add(@RequestBody Video video) {
        log.info("新增视频参数"+video);
        SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
        video.setUserid(sysLoginUser.getId());
        video.setUsername(sysLoginUser.getUsername());
        video.setStatus(0);
        videoMapper.insert(video);
        return new SuccessResponseData();
    }

    /**
     * 编辑
     * @param video
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public ResponseData update(@RequestBody Video video) {
        SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
        video.setUserid(sysLoginUser.getId());
        video.setUsername(sysLoginUser.getUsername());
        videoMapper.updateById(video);
        return new SuccessResponseData();
    }

    /**
     * 查询详情
     * @param video
     * @return
     */
    @GetMapping("detail")
    @ResponseBody
    public ResponseData detail(Video video) {
        return new SuccessResponseData(videoMapper.selectById(video.getId()));
    }

    /**
     *  删除
     * @param list
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public ResponseData delete(@RequestBody List<Video> list) {
        for (Video video : list) {
            videoMapper.deleteById(video.getId());
        }
        return new SuccessResponseData();
    }

    /**
     * 发布 撤销
     * @param video
     * @return
     */
    @PostMapping("changeStatus")
    @ResponseBody
    public ResponseData changeStatus(@RequestBody Video video){
        if (ObjectUtils.isEmpty(video)){
            return ResponseData.error("参数异常");
        }
        Video video1 = new Video();
        video1.setId(video.getId());
        video1.setStatus(video.getStatus());
        videoMapper.updateById(video1);
        return ResponseData.success();
    }


}
